package gui;


import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.*;
import javax.swing.border.EtchedBorder;

import users.Clerk;

public class PurchaseInStoreGui extends JPanel{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4241575775048921734L;

	private Clerk clerk;
	
	// the component that sent to a Clerk Tabbed panel. 
	
	// panel for purchase
	private JPanel jp1;
	//panel for return 
	private JPanel  jp2;

	// the text field takes upc, date, card#, expire.
	private JTextField storeName, purchaseDate, itemList, cardNumber, expireDate;
	
	// where receipt should be printed out
	private JTextArea textArea;
	
	private JScrollPane scrollPane;
	
	// the component that contains most of panels. 
	private JPanel jpMain;
	
	private JButton purchaseButton;
	
	private String storeNameInput;
	private String itemTypeInput;
	private String purchaseDateInput;
	private String cardNumberInput;
	private String expireDateInput;
	
	public PurchaseInStoreGui(Clerk clerk)
	{
		this.clerk = clerk;
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		init();
	}
	public void init()
	{
				
		initJp1();
		initJp2();
		addListeners();
		
		
		
	}
	
	public void initJp1()
	{
		
		
		// setting up for purchase information panel
		jp1 = new JPanel();
		jp1.setLayout(new GridLayout (5,2));
		jp1.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));
		jp1.add( new JLabel("Item upc: "));
		
		
		// setting up textField for item UPCs
		itemList = new JTextField();
        itemList.setPreferredSize(new Dimension(300, 30));
        jp1.add(itemList);
        
              
        // setting up textField for item purchaseDate
        
        jp1.add( new JLabel("StoreName: "));
        storeName = new JTextField();
        storeName.setPreferredSize(new Dimension(300,30));
        jp1.add(storeName);
        
               
        //setting up textField for customers' card number
        jp1.add(new JLabel("CardNumber :"));
        cardNumber = new JTextField();
        cardNumber.setPreferredSize(new Dimension(300,30));
        jp1.add(cardNumber);
        
        //setting up textField for customers cards' expireDate
        jp1.add(new JLabel("Expire Date(yyyy-mm-dd):"));
        expireDate = new JTextField();
        expireDate.setPreferredSize(new Dimension(300,30));
        jp1.add(expireDate);
        
        //setting up Button 
        purchaseButton = new JButton("Purchase");
        purchaseButton.setPreferredSize(new Dimension(300,30));
        
        jp1.add(purchaseButton);
        
        add(jp1);
	
		
		
	}
	
	public void initJp2()
	{
		JPanel jp2 = new JPanel();
		textArea = new JTextArea(20, 1);
		scrollPane = new JScrollPane(textArea);
		
		// create ScrollPane inside TextArea
		jp2.setLayout(new BorderLayout());
		jp2.add(scrollPane, BorderLayout.CENTER);
		
		
		add(jp2);
	}

	public void addListeners()
	{
		purchaseButton.addActionListener(new ActionListener(){
			
			public void actionPerformed(ActionEvent e) {
				boolean flag = false;
				// splitting text fields into an array so that it can be differentiated. 
				System.out.print(itemList.getText());
				String[] text = itemList.getText().split(", *");
				
				
				// retrieving text field input
		
				cardNumberInput =  cardNumber.getText();
				expireDateInput =  expireDate.getText();
				storeNameInput = storeName.getText();
				
				// check weather credit cars number is correctly input
				
				if(cardNumberInput.length() == 16 || cardNumberInput.length() ==0)
				{
				try {
					clerk.searchingPrices(text, cardNumberInput, expireDateInput, storeNameInput);
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					textArea.append("************************************************" + "\n" 
							+ "WRONG Input" + "\n" +" ***************************************** ");
					flag = true;
				}
				
				try {
					clerk.insertIntoPurchaseItem();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
					textArea.append("************************************************" + "\n" 
							+ "WRONG Input" + "\n" +" ***************************************** ");
					flag = true;
				}
				// show receipt to the customer. 
				
				int[] queryItemint = clerk.queryReceiptint();
				
				// store information into Stored Table;
				
				if (flag == false)
				{
				
				textArea.append("******************* Receipt ******************" + "\n" +
						"\n" + "Receipt ID: "  + queryItemint[0] +
						"\n" + "Date: 2008-07-25" +
						"\n" + "Items and prices: ");
				
				String [] queryItemstring = clerk.queryReceiptstring();
				int[] quantityItem = clerk.queryQuantity();
				for (int i = 0; i<queryItemstring.length; i++)
				{
					if(queryItemstring[i] != null)
					textArea.append( queryItemstring[i] + " quantity: " +  quantityItem[i] + "\n" );
					else
						break;
				}	
				
				textArea.append(
						"Total Amount:  " + queryItemint[1] + "\n");
					
					if(cardNumberInput.length()>=10)
					{
					textArea.append("Card number : " + cardNumberInput.substring(0,5) +
				

						"\n\n" + 
						"*********************************************************" + "\n\n");
	
					}
				
				
			}
				else	
				{
					{
						textArea.append("*************************************** " + "\n"
							+ "Wrong input" + "\n" + "**************************************" + "\n");
					}
						
					
				}
				}
		}	
		});
	}
		
}